<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="触发器名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入触发器名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="equipmentType">
        <el-cascader
          :options="devices"
          @change="cascaderPropshhqfun"
          :props="cascaderProps"
          placeholder="请选择设备类型"
          clearable
        ></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:list:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:list:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:list:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:list:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="listList" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="55" label="序号" align="center" />
      <el-table-column label="触发器名称" align="center" prop="name" />
      <el-table-column label="设备类型" align="center" prop="dname"></el-table-column>
      <el-table-column label="所属区域" align="center" prop="region" />
      <el-table-column label="数据类型" align="center" prop="dataType" >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.data_type"
            :value="scope.row.dataType"
          />
        </template>
      </el-table-column>
      <el-table-column label="触发条件" align="center" prop="condition" >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.trigger_type"
            :value="scope.row.condition"
          />
        </template>
      </el-table-column>
      <el-table-column label="参数" align="center" prop="parameter" />
      <el-table-column label="状态" align="center" key="status"  prop="status">
        <template slot-scope="scope">
            <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value= '1'
            :inactive-value= '0'
            @change="handleStatusChange(scope.row,scope.$index)"
            >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:list:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改触发器列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="30%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="触发器名称" prop="name" class="cname">
          <el-input v-model="form.name" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceName" >
          <el-cascader
            :options="devices"
            v-model="equipmentTypeData"
            :props="cascaderProps"
            placeholder="请选择设备类型"
          ></el-cascader>
        </el-form-item>

        <el-form-item label="所属区域"  prop="region">
          <el-cascader
            size="large"
            :options="options"
            v-model="form.region"
            @change="handleChange">
          </el-cascader>
        </el-form-item>
        <el-form-item label="数据类型" prop="dataType">
          <el-select v-model="form.dataType" placeholder="请选择数据类型" clearable>
            <el-option
              v-for="item in dict.type.data_type"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="触发类型" prop="condition">
          <el-select v-model="form.condition" placeholder="请选择触发类型" clearable>
            <el-option
              v-for="item in dict.type.trigger_type"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参数" prop="parameter">
          <el-input-number placeholder="请输入参数" v-model="form.parameter" controls-position="right"  :min="1" :max="100"></el-input-number>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio v-model="form.status" label="1">启用</el-radio>
          <el-radio v-model="form.status" label="0">禁用</el-radio>
        </el-form-item>
        <el-form-item label="报警恢复触发" prop="recoveryTrigger">
          <el-radio v-model="form.recoveryTrigger" label="1">是</el-radio>
          <el-radio v-model="form.recoveryTrigger" label="0">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listList, getList, delList, addList, updateList ,changeListStatus,listDeviceName} from "@/api/system/list";
import Treeselect from "@riophae/vue-treeselect";
import { pcaTextArr } from "element-china-area-data";
// import {listDeviceName} from "@/api/system/manage";

export default {
  name: "List",
  dicts:['device_type','data_type','trigger_type'],
  components: { Treeselect },
  data() {
    return {
      //设备类型
      devices:[],
      equipmentTypeData:[],
      value:false,
      options: pcaTextArr ,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 触发器列表表格数据
      listList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        equipmentType: null,
        regionId: null,
        region: null,
        dataType: null,
        condition: null,
        parameter: null,
        status: null,
        enable: null,
        recoveryTrigger: null
      },
      // 表单参数
      form: {},
      cascaderProps: {
        value: 'groupId', // 设备类型对象的value属性名
        label: 'groupName', // 设备类型对象的label属性名
        children: 'devTypes', // 设备类型对象的子级数据属性名
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: '请填写触发器名称', trigger: 'blur' }
        ],
        equipmentType: [
          { required: true, message: "请选择设备类型", trigger: "change" },
        ],
        region: [
          { required: true, message: "请选择所属区域", trigger: "change" },
        ],

        dataType: [
          { required: true, message: "请选择数据类型", trigger: "change" },
        ],

        condition: [
          { required: true, message: "请选择触发器类型", trigger: "change" },
        ],

        parameter: [
          { required: true, message: "请填写参数", trigger: "blur" },
        ],

        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ],

        recoveryTrigger: [
          { required: true, message: '请选择报警报警数据触发', trigger: 'change' }
        ],

      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    //设备类型
    listDeviceName(null).then(response => {
      this.devices = response.rows;
      console.log(this.devices)
      this.total = response.total;
      this.loading = false;
    });
  },
  methods: {
    cascaderPropshhqfun(res){
      this.queryParams.equipmentType = res[1]
      this.queryParams.devTypeId = res[0]
    },
    /** 查询触发器列表列表 */
    getList() {
      this.loading = true;
      listList(this.queryParams).then(response => {
        this.listList = response.rows;
        console.log(this.listList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        equipmentType: null,
        regionId: null,
        region: null,
        dataType: null,
        condition: null,
        parameter: null,
        status: null,
        enable: null,
        recoveryTrigger: null
      };
      this.resetForm("form");
    },

    // 触发器状态
    handleStatusChange(row,index) {
      console.log(row.status)
      let that = this
      let text = row.status === 0 ? "停用" : "启用";
      this.$modal.confirm('确认要' + text + '' + row.name + '设备吗？').then(function() {
        return changeListStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        console.log(that.listList)
        if(that.listList[index].status==0){
          that.listList[index].status=1
        }else{
          that.listList[index].status=0
        }
        //  that.listList[index].status=row.status == 0 ? "0" : "1";
        console.log(that.listList[index].status)

       // row.status =
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加触发器列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getList(id).then(response => {
        this.form = response.data;
        console.log(this.form)
        this.form.recoveryTrigger = this.form.recoveryTrigger + "";
        this.form.status=response.data.status + "";
        this.form.region = response.data.region.split("/");
        this.open = true;
        this.title = "修改触发器列表";
        this.equipmentTypeData[0] = this.form.devTypeId
        this.equipmentTypeData[1] = Number(this.form.equipmentType)
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.region = Array.isArray(this.form.region)?this.form.region.join("/"):this.form.region;
          this.form.equipmentType = this.equipmentTypeData[1] ? this.equipmentTypeData[1] :''
          this.form.devTypeId = this.equipmentTypeData[0] ? this.equipmentTypeData[0] :''
          if (this.form.id != null) {
            updateList(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addList(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).catch((err)=>{
              // this.form.region = this.form.region.join('/');
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除触发器列表编号为"' + ids + '"的数据项？').then(function() {
        return delList(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      debugger
      this.download('system/list/export', {
        ...this.queryParams
      }, `list_${new Date().getTime()}.xlsx`)

    },
    handleChange (value) {
    },
  }
};
</script>
<style>
.cname{
  width: 320px;
}
</style>

